#!/usr/bin/python
######
#       opener.py
#       Copyright Etelos Inc. 2007
#       Darien Hager
#       darien@etelos.com
######
import sys, os



def openFile(filepath):

		if(not os.path.isfile(filepath)):
			return False
		
		PLAT_WIN = 0
		PLAT_MAC = 1
		PLAT_LINUX = 2
	
		mapping = {'darwin':PLAT_MAC,
		'linux2':PLAT_LINUX,
		'win32':PLAT_WIN
		
		}
		plat = None
		if(sys.platform in mapping):
			plat = mapping[sys.platform]			
		else:
			return False
			
			
		if(plat == PLAT_WIN):
			try:
				os.startfile(os.path.normpath(filepath))
				return True
			except AttributeError:
				assert False, "Error in platform detection, os.startfile() not available"
				return False;
			except WindowsError:
				return False
				
		elif(plat == PLAT_MAC):
			
			#pid = os.spawnlp(os.P_NOWAIT, 'open', 'open', filepath)
			#if not(pid > 0):
			#	assert False, "TODO notify user or open folder or something"
			val = os.system("open \""+filepath+"\"")
			if(val == 0):
				return True
			else:
				return False		
		elif(plat == PLAT_LINUX):
			val = os.system("gnome-open \"" + filepath + "\"")
			if(val == 0):
				return True
			else:
				# Try the KDE version?
				val = os.system("kfmclient exec \"" + filepath + "\"")
				if(val == 0):
					return True
				else:
					return False
	
		else:
			assert False, "Platform mapped to unsupported system type"
			return False